/**
 * 市场树视图
 * 负责在网页上市场树
 */

function MarketTreeView(c) {
    var _this = this;
    var controller;

    var init = function(c) {
            controller = c;
        };
    _this.createControls = function () {
        // 显示市场导航
        controller.showMarketTree();
        // 点击li标签时显示子树
        $("li").live("click",controller.showSubTree);
    }
    _this.getId = function (element) {
        return $(element).attr('id');
    }
    _this.getHastypes = function (element) {
        return $(element).attr('hastypes')
    }
    _this.drawMarketTree = function(result) {
            var menu;
            $.each(result, function(i, type) {
                menu += "<li class='closed' id=" + type['id'] + " hastypes=" + type['hastypes'] + "><span class='folder'>" + type['name'] + "</span><ul class='filetree treeview-famfamfam' id='sub_" + type['id'] + "'></ul></li>";
            });
            $(menu).appendTo("#market_tree");
            $("#market_tree").treeview({
                add: menu
            });
        };
    _this.drawSubTree = function(pid, hastypes, result) {
            var menu;
            $.each(result, function(i, type) {
                if(hastypes == 0) {
                    menu += "<li class='closed' id=" + type['id'] + " hastypes=" + type['hastypes'] + "><span class='folder'>" + type['name'] + "</span><ul class='filetree treeview-famfamfam' id='sub_" + type['id'] + "'></ul></li>";
                } else {
                    menu += "<li class='closed'><span class='file'><a class='item' id=" + type['id'] + " techlevel=" + type['techlevel'] + " wastefactor=" + type['wastefactor'] + " href='#'>" + type['name'] + "</a></span></li>";
                }
            });
            $(menu).appendTo("#sub_" + pid);
            $("#sub_" + pid).treeview({
                add: menu
            });
        };
    // run construct function
    init(c);
}

/**
 * 价格视图
 * 负责显示物品造价，包含制造所需材料
 */

function PriceView(c,m) {
    var _this = this;
    var controller;
    var model;

    // construct function
    var init = function(c,m) {
        controller = c;
        model = m;
        // 注册成为观察者
        model.registerObserver(_this);
    }
    // private function
    _this.createControls = function () {
        $("#doprice").click(controller.buttonClicked);
        $(".item").live("click",controller.backGroundLoad);
    }
    /**
     * 获取元素的id
     * @param  {object} element 当前选中的元素
     * @return {int}      元素的属性 id 
     */
    _this.getId = function (element) {
        return $(element).attr('id');
    }
    /**
     * 获取元素的名称
     * @param  {object} element 当前选中元素 
     * @return {string}        当前选中的物品的名称 
     */
    _this.getName = function (element) {
        return $(element).html();
    }
    /**
     * 设置物品名称
     * @param {string} name
     */
    _this.setItemName = function (name) {
        $("#itemname").val(name);
    }
    /**
     * 获取材料等级
     * @return {int} 蓝图的材料等级 
     */
    _this.getMl = function () {
        return parseInt($("#ml").val());
    }
    /**
     * 获取技能等级
     * @return {int}
     */
    _this.getSl = function () {
        return $("#isl").val();
    }
    /**
     * 获取损耗系数
     * @param  {int} id 物品id
     * @return {int}    
     */
    _this.getWf = function (id) {
        return $(".item[id='"+id+"']").attr('wastefactor');
    }
    /**
     * 显示等待信息
     */
    _this.showWait = function () {
        $(".table").html("正在加载请稍等...");
    }
    /**
     * 获取科技等级
     * @param  {object} element 当前选中元素 
     * @return {int}        1:t1科技 2:t2科技 
     */
    _this.getTechLevel = function (element) {
        return $(element).attr("techlevel");
    }
    /**
     * 显示错误信息
     * @param  {string} msg 错误的具体描述
     */
    _this.showErrorMessage = function (msg) {
        alert(msg);
    }
    _this.showT2InputView = function () {
        $(".t2").show();
    }
    _this.hideT2InputView = function () {
        $(".t2").hide();
    }
    /**
     * 显示物品材料信息和造价
     * 这里的materials增加了一个属性materials[i]['price'] // 价格小计
     * @param  {materials} materials 详细定义才参照eve_controller.js中的requestMaterials 
     * @param {float} total_price 材料的总价 
     */
    var drawMeaterials = function(materials,total_price) {
        var table = "<table align='center' border='1'><tr>";
        if(materials == 'FALSE') {
            $(".table").html('没有该物品的材料信息');
            return;
        }
        table += "<th>材料名称</th><th>需求数量</th><th>每轮损耗</th>";
        table += "<th>材料单价</th><th>价格小计</th></tr>";
        $.each(materials, function(i, item) {
            table += "<tr>";
            table += "<td>" + item['name'] + "</td>";
            table += "<td>" + item['quantity'] + "</td>";
            table += "<td>" + item['damage_per_job'] + "</td>";
            table += "<td>" + item['sell'] + "</td>";
            table += "<td>" + item['price'] + "</td>";
            table += "</tr>";
        });
        table += "<tr><th>材料总价</th><th>"+total_price+"</th></tr>";
        table += "</table>";
        $(".table").html(table);
    };
    /**
     * 实现观察者的方法
     */
    _this.update = function () {
        var materials = controller.getProducePrice();
        var total_price = controller.getProduceTotalPrice();
        drawMeaterials(materials,total_price);
    }
    init(c,m);
}